package dice.sinanya.db.groupSwitch;

import dice.sinanya.db.tools.DbUtil;
import dice.sinanya.entity.EntityGroupSwitch;
import dice.sinanya.entity.imal.AlgorithmTypes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static dice.sinanya.system.MessagesBanList.groupSwitchHashMap;
import static dice.sinanya.system.MessagesSystem.loginInfo;

/**
 * @author SitaNya
 * 日期: 2019-08-07
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明:
 */
public class SelectGroupSwitch {
    private static final Logger log = LogManager.getLogger(SelectGroupSwitch.class.getName());


    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public void flushGroupSwitchFromDataBase() {
        groupSwitchHashMap.clear();
        try (Connection conn = DbUtil.getConnection()) {
            String sql = "select * from test.groupSwitch where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setLong(1, loginInfo.getLoginId());
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        EntityGroupSwitch entityGroupSwitch = new EntityGroupSwitch();
                        entityGroupSwitch.setJrrp(set.getBoolean("jrrp"));
                        entityGroupSwitch.setNpc(set.getBoolean("npc"));
                        entityGroupSwitch.setGas(set.getBoolean("gas"));
                        entityGroupSwitch.setBg(set.getBoolean("bg"));
                        entityGroupSwitch.setTz(set.getBoolean("tz"));
                        entityGroupSwitch.setSimple(set.getBoolean("simple"));
                        entityGroupSwitch.setOb(set.getBoolean("ob"));
                        entityGroupSwitch.setDeck(set.getBoolean("deck"));
                        entityGroupSwitch.setRule(set.getInt("rule"));
                        entityGroupSwitch.setZookeeper(set.getBoolean("zookeeper"));
                        entityGroupSwitch.setAlgorithmTypes(AlgorithmTypes.getValueByCode(set.getInt("algorithm")));
                        entityGroupSwitch.setSecret(set.getBoolean("secret"));
                        entityGroupSwitch.setReply(set.getBoolean("reply"));
                        groupSwitchHashMap.put(set.getLong("groupId"), entityGroupSwitch);
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }
}
